#!/bin/sh
# preinst script for wazuh-agent

set -e

# configuration variables
DIR="/var/ossec"
WAZUH_TMP_DIR="${DIR}/packages_files/agent_config_files"

# environment configuration
if [ ! -d ${WAZUH_TMP_DIR} ]; then
    mkdir -p ${WAZUH_TMP_DIR}
else
    rm -rf ${WAZUH_TMP_DIR}
    mkdir -p ${WAZUH_TMP_DIR}
fi

case "$1" in
    install|upgrade)

        if [ "$1" = "upgrade" ]; then

            if [ ! -d "$DIR" ]; then
                echo "Error: Directory $DIR does not exist. Cannot perform upgrade" >&2
                exit 1
            fi

            if command -v systemctl > /dev/null 2>&1 && systemctl > /dev/null 2>&1 && systemctl is-active --quiet wazuh-agent > /dev/null 2>&1; then
                systemctl stop wazuh-agent.service > /dev/null 2>&1
                touch ${WAZUH_TMP_DIR}/wazuh.restart
            elif command -v service > /dev/null 2>&1 && service wazuh-agent status 2>/dev/null | grep "is running" > /dev/null 2>&1; then
                service wazuh-agent stop > /dev/null 2>&1
                touch ${WAZUH_TMP_DIR}/wazuh.restart
            fi
            
            if [ -d ${DIR}/logs/ossec ]; then
                mv ${DIR}/logs/ossec ${DIR}/logs/wazuh
            fi
            
            if [ -d ${DIR}/queue/ossec ]; then
                mv ${DIR}/queue/ossec ${DIR}/queue/sockets
            fi
        fi

        if [ ! -z "$2" ] && [ ! -f ${DIR}/etc/ossec.conf ] ; then
            touch ${WAZUH_TMP_DIR}/create_conf
        fi

        # Delete old service
        if [ -f /etc/init.d/ossec ]; then
            rm /etc/init.d/ossec
        fi
        # back up the current user rules
        if [ -f ${DIR}/etc/client.keys ]; then
            cp ${DIR}/etc/client.keys ${WAZUH_TMP_DIR}/client.keys
        fi
        if [ -f ${DIR}/etc/local_internal_options.conf ]; then
            cp -p ${DIR}/etc/local_internal_options.conf ${WAZUH_TMP_DIR}/local_internal_options.conf
        fi
        if [ -f ${DIR}/etc/ossec.conf ]; then
            cp -p ${DIR}/etc/ossec.conf ${WAZUH_TMP_DIR}/ossec.conf
        fi

        if [ -d ${DIR}/etc/shared ]; then
            files="$(ls -A ${DIR}/etc/shared/*)"
        fi

        if [ ! -z "$files" ]; then
            mkdir -p ${WAZUH_TMP_DIR}/group
            cp -rp ${DIR}/etc/shared/* ${WAZUH_TMP_DIR}/group/
        fi
    ;;

    abort-upgrade)

    ;;

    *)
        echo "preinst called with unknown argument \`$1'" >&2
        exit 1

    ;;

esac

exit 0
